<script setup lang="ts">
import { ref, onMounted, onUnmounted, nextTick, watch } from 'vue'
import { useRoute } from 'vue-router'

const sidebarCollapsed = ref(false)
const contentArea = ref<HTMLElement>()
const route = useRoute()

// 控制子菜单的展开/折叠状态
const expandedMenus = ref<string[]>(['material', 'model', 'analytics'])

const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

const toggleSubMenu = (menuKey: string) => {
  const index = expandedMenus.value.indexOf(menuKey)
  if (index > -1) {
    expandedMenus.value.splice(index, 1)
  } else {
    expandedMenus.value.push(menuKey)
  }
}

const isMenuExpanded = (menuKey: string) => {
  return expandedMenus.value.includes(menuKey)
}

// 处理内容区域滚动
const handleContentScroll = () => {
  if (contentArea.value) {
    const element = contentArea.value
    const hasScroll = element.scrollHeight > element.clientHeight
    const isScrolling = element.scrollTop > 0

    if (hasScroll && isScrolling) {
      element.classList.add('scrolling')
    } else {
      element.classList.remove('scrolling')
    }
  }
}

// 检查是否需要滚动条
const checkScrollbar = () => {
  nextTick(() => {
    if (contentArea.value) {
      const element = contentArea.value
      const hasScroll = element.scrollHeight > element.clientHeight

      if (hasScroll) {
        element.classList.add('has-scroll')
      } else {
        element.classList.remove('has-scroll')
      }
    }
  })
}

// 监听路由变化
watch(
  () => route.path,
  () => {
    // 路由变化时重新检查滚动条
    setTimeout(checkScrollbar, 100)
  },
)

onMounted(() => {
  // 监听内容区域滚动
  if (contentArea.value) {
    contentArea.value.addEventListener('scroll', handleContentScroll)
  }

  // 初始检查
  checkScrollbar()

  // 监听窗口大小变化
  window.addEventListener('resize', checkScrollbar)
})

onUnmounted(() => {
  window.removeEventListener('resize', checkScrollbar)

  if (contentArea.value) {
    contentArea.value.removeEventListener('scroll', handleContentScroll)
  }
})
</script>

<template>
  <div class="app-container">
    <!-- 顶部导航 -->
    <div class="header">
      <div class="logo">
        <img src="/logo.png" alt="Logo" />
        <span>AI 图文助手</span>
      </div>
      <div class="search-container">
        <el-input placeholder="搜索素材ID/用户昵称/标签关键词" class="search-input">
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary">搜索</el-button>
      </div>
      <div class="user-info">
        <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"></el-avatar>
        <span>马大千</span>
        <el-button plain size="small">
          <el-icon><SwitchButton /></el-icon>
          退出登录
        </el-button>
      </div>
    </div>

    <!-- 中间内容区域 -->
    <div class="main-content">
      <!-- 左侧导航栏 -->
      <div class="sidebar" :class="{ collapsed: sidebarCollapsed }">
        <div class="sidebar-header">
          <button class="toggle-btn" @click="toggleSidebar">
            <el-icon><Fold v-if="!sidebarCollapsed" /><Expand v-else /></el-icon>
          </button>
        </div>

        <nav class="sidebar-nav">
          <!-- 素材管理 -->
          <div class="nav-group">
            <div class="nav-group-title" @click="toggleSubMenu('material')">
              <el-icon><Folder /></el-icon>
              <span class="nav-text" v-show="!sidebarCollapsed">素材管理</span>
              <el-icon class="arrow-icon" v-show="!sidebarCollapsed">
                <ArrowDown v-if="isMenuExpanded('material')" />
                <ArrowRight v-else />
              </el-icon>
            </div>
            <div class="sub-menu" v-show="!sidebarCollapsed && isMenuExpanded('material')">
              <router-link to="/chat" class="nav-item sub-nav-item">
                <el-icon><Files /></el-icon>
                <span class="nav-text">素材库总览</span>
              </router-link>
              <router-link to="/history" class="nav-item sub-nav-item">
                <el-icon><Timer /></el-icon>
                <span class="nav-text">操作日志</span>
              </router-link>
            </div>
          </div>

          <!-- 模型配置 -->
          <div class="nav-group">
            <div class="nav-group-title" @click="toggleSubMenu('model')">
              <el-icon><Monitor /></el-icon>
              <span class="nav-text" v-show="!sidebarCollapsed">模型配置</span>
              <el-icon class="arrow-icon" v-show="!sidebarCollapsed">
                <ArrowDown v-if="isMenuExpanded('model')" />
                <ArrowRight v-else />
              </el-icon>
            </div>
            <div class="sub-menu" v-show="!sidebarCollapsed && isMenuExpanded('model')">
              <router-link to="/api" class="nav-item sub-nav-item">
                <el-icon><Connection /></el-icon>
                <span class="nav-text">图像识别API</span>
              </router-link>
              <router-link to="/generation" class="nav-item sub-nav-item">
                <el-icon><Edit /></el-icon>
                <span class="nav-text">文案生成模型</span>
              </router-link>
            </div>
          </div>

          <!-- 数据看板 -->
          <div class="nav-group">
            <div class="nav-group-title" @click="toggleSubMenu('analytics')">
              <el-icon><DataAnalysis /></el-icon>
              <span class="nav-text" v-show="!sidebarCollapsed">数据看板</span>
              <el-icon class="arrow-icon" v-show="!sidebarCollapsed">
                <ArrowDown v-if="isMenuExpanded('analytics')" />
                <ArrowRight v-else />
              </el-icon>
            </div>
            <div class="sub-menu" v-show="!sidebarCollapsed && isMenuExpanded('analytics')">
              <router-link to="/core" class="nav-item sub-nav-item">
                <el-icon><Cpu /></el-icon>
                <span class="nav-text">核心数据概览</span>
              </router-link>
              <router-link to="/user" class="nav-item sub-nav-item">
                <el-icon><User /></el-icon>
                <span class="nav-text">用户行为洞察</span>
              </router-link>
            </div>
          </div>
        </nav>
      </div>

      <!-- 右侧内容区域 -->
      <div class="content-area" ref="contentArea">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  overflow: hidden;
  background-color: #f5f7fa;
}

.header {
  height: 60px;
  background-color: #fff;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  z-index: 100;

  .logo {
    display: flex;
    align-items: center;
    margin-right: 20px;
    
    img {
      height: 30px;
      margin-right: 10px;
    }
    
    span {
      font-size: 16px;
      font-weight: 600;
      color: #409EFF;
    }
  }

  .search-container {
    flex: 1;
    display: flex;
    max-width: 500px;
    margin: 0 20px;
    
    .search-input {
      margin-right: 10px;
    }
  }

  .user-info {
    display: flex;
    align-items: center;
    
    .el-avatar {
      margin-right: 10px;
    }
    
    span {
      margin-right: 15px;
      font-size: 14px;
    }
  }
}

.main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.sidebar {
  width: 220px;
  background-color: #fff;
  border-right: 1px solid #e6e6e6;
  transition: width 0.3s ease;
  overflow-y: auto;

  &.collapsed {
    width: 64px;
    
    .nav-group-title {
      padding: 12px 0;
      justify-content: center;
      
      .el-icon {
        margin-right: 0;
      }
    }
  }

  .sidebar-header {
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 1px solid #f0f0f0;

    .toggle-btn {
      width: 30px;
      height: 30px;
      border: none;
      background: transparent;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #909399;
      
      &:hover {
        color: #409EFF;
      }
    }
  }

  .sidebar-nav {
    padding: 10px 0;
    
    .nav-group {
      margin-bottom: 5px;
      
      .nav-group-title {
        display: flex;
        align-items: center;
        padding: 12px 20px;
        color: #303133;
        font-weight: 600;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:hover {
          background-color: #f5f7fa;
          color: #409EFF;
        }
        
        .el-icon {
          font-size: 18px;
          margin-right: 12px;
        }
        
        .arrow-icon {
          margin-left: auto;
          font-size: 14px;
          margin-right: 0;
        }
      }
      
      .sub-menu {
        background-color: #f8f9fa;
        
        .sub-nav-item {
          padding-left: 40px;
          font-weight: normal;
        }
      }
    }

    .nav-item {
      display: flex;
      align-items: center;
      padding: 10px 20px;
      text-decoration: none;
      color: #606266;
      transition: all 0.3s ease;
      
      .el-icon {
        font-size: 16px;
        margin-right: 12px;
      }

      &:hover {
        background-color: #f5f7fa;
        color: #409EFF;
      }

      &.router-link-active {
        background-color: #ecf5ff;
        color: #409EFF;
        border-left: 3px solid #409EFF;
      }

      .nav-text {
        font-size: 14px;
      }
    }
  }
}

.content-area {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  background-color: #f5f7fa;

  // 默认隐藏滚动条
  scrollbar-width: none;
  -ms-overflow-style: none;

  &::-webkit-scrollbar {
    width: 6px;
    background: transparent;
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }

  &::-webkit-scrollbar-thumb {
    background: transparent;
    border-radius: 3px;
    transition: background-color 0.3s ease;
  }

  // 有滚动内容时显示滚动条
  &.has-scroll {
    &.scrolling {
      &::-webkit-scrollbar-thumb {
        background: rgba(0, 0, 0, 0.2);

        &:hover {
          background: rgba(0, 0, 0, 0.4);
        }
      }
    }
  }
}
</style>